<?php

/**
 * RabbitCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rabbitcms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Role.php 5028 2010-08-28 16:44:55Z huuphuoc $
 * @since		2.0.5
 */
class Core_Models_Role extends Rabbit_Db_Table_Abstract implements Core_Models_Interfaces_Role {

    protected $_name = 'core_role';

    public function convert($entity) {
        return new Core_Models_Role($entity);
    }

    public function getAclRoles() {
        return $this->fetchAll();
    }

    public function getRoles() {
        return $this->fetchAll();
    }

    public function getById($id) {
        $sql = sprintf("SELECT * FROM " . $this->_prefix . "core_role
						WHERE role_id = '%s' LIMIT 1",
                        mysql_real_escape_string($id));
        $rs = mysql_query($sql);
        $return = (0 == mysql_num_rows($rs)) ? null : new Core_Models_Role(mysql_fetch_object($rs));
        mysql_free_result($rs);
        return $return;
    }

    public function add($params) {
        try{
            $role = $this->createRow($params);
            return $role->save();
        }catch(Exception $ex){
            return false;
        }
    }

    public function toggleLock($id) {
        try{
            $db = $this->getAdapter();
            //return $db->query("UPDATE ". $this->_name. " SET locked = 1 - locked WHERE id = ".$id)->execute();
            //return $db->update($this->_name, array('locked' => '1 - locked'), 'id =' . $id);
            return $this->update(array('locked' => new Zend_Db_Expr('1 - locked')), 'id =' . $id );
        }catch(Exception $ex){
            echo $ex->getMessage();
            return false;
        }
    }

    public function delete($id) {
        return parent::delete('id = ' .$id);
    }

    public function getRolesIncludeUser() {
        try {
            $db = $this->getAdapter();
            $select = $db->select()
                            ->from(array("r" => $this->_name))
                            ->joinLeft(
                                    array("u" => $this->_prefix . 'core_user'),
                                    'r.id = u.role_id',
                                    array('num_users' => 'COUNT(u.id)'))
                            ->group('r.id');
            return $db->query($select)->fetchAll();
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return null;
        }
    }

    public function countUsers($roleId) {
        return $this->getAdapter()->select()
                ->from($this->_prefix.'core_user', array('COUNT(id)'))
                ->where('role_id = ?', $roleId)
                ->query()->fetchColumn(0);
    }

}
